export default {
	data() {
		return{
      SRC: '',
      /*当前页显示数量*/
      pageSize:10,
      /*每页显示的数量*/
      pageSizes:[10, 20, 50, 100],
      /*dataTotal表数量*/
      dataTotal:0,
      /*分页器*/
      currentPage: 1,
      // form输入框的胡宽度
      formLabelWidth:"150px",
			/*initWidth 的宽*/
      initWidth:'100%',
      // 添加或搜索数据
      region: [],
      ruleForm: {
        enumName:"",
        enumValue:"",
        description:"",
        gameName:'',
        gameId: ''
      },
      // 游戏列表数据
      projects: [],
      // 表格数据
      tableData: [],
      tableDataAll: [],
      // 修改弹窗出现
      revampShow: false,
      // 默认的工程
      revampRegion: '',
      // 修改弹窗数据
      revampForm: {
        enumName:"",
        enumValue:"",
        description:"",
        gameName:'',
        gameId: ''
      },
      revampRules: {
        id: [
          { type: 'number', message: '请输入正确的数字', trigger: 'blur' }
        ]
      }
		}
	},
	methods: {
    // 通用 成功 与 失败
    successFun(data) {
      this.$message({
        message: data,
        type: 'success'
      });
    },
    errorFun(data) {
      this.$message.error(data);
    },
    // 分页器
    // 控制每页的数量
    handleSizeChange(val) {
      this.pageSize = val;
      this.getPageData(`curPage=${this.currentPage}&pageSize=${this.pageSize}`);
    },
    // 页数
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getPageData(`curPage=${this.currentPage}&pageSize=${this.pageSize}`);
    },
		 /*设置初始值*/
    _init(){
      window.addEventListener('resize', function() {
        /*修改表格的宽度*/
            this.initWidth = document.body.clientWidth-220+"px";
        }.bind(this))
    }, 
    // 选择游戏
    projectsChange(val) {
      var p = this.projects;
      for(var i =0; i<p.length; i++){
        if(val === p[i].value){
          this.ruleForm.gameName = p[i].label;
          this.ruleForm.gameId = p[i].value;
        }
      }
    },
    // 修改用户
    userEdit(index, row){
      this.revampForm = row;
      this.revampForm.password = '';
      this.userShow = true;
    },
    // 编辑权限
    edit(index, row) {
      for(var i in row){
        this.revampForm[i] = row[i];
      }
      this.revampRegion = this.revampForm.gameName;
    	this.revampShow = true;
    },
    // 删除用户
    handleDelete(index, row) {
    },
    // 添加系统配置
    //http://10.236.44.188:8080/sysconfig/add.do?enumName=1&enumValue=1.1&gameName=game1&gameId=201&description=111
    addMessage() {
      /*var obj = this.ruleForm;*/
      var obj = {
        gameId:`${this.ruleForm.gameId},`,
        enumName:this.ruleForm.enumName,
        enumValue:this.ruleForm.enumValue,
        description:this.ruleForm.description
      }
      console.log(obj)
      this.$http.post(`${this.SRC}sysconfig/add.do`,obj,{emulateJSON:true}).then(
        function(res){
          if(res.body['code'] === 0){
            this.successFun(res.body["msg"]);
            this.getPageData(`curPage=${this.currentPage}&pageSize=${this.pageSize}`);
          }else{
            this.errorFun(res.body["msg"])
          }
        }
      )
    },
    // 添加提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.addMessage();
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    //搜索提交
    findForm() {
      var obj = this.ruleForm;
      var str = '';
      for(var i in obj){
        if(obj[i] !== "" && typeof(obj[i])!=='object'){
          str += `${i}=${obj[i]}&`
          console.log(str)
        }
      }
      str = str.substring(0,str.length-1);
      this.getPageData(`curPage=${this.currentPage}&pageSize=${this.pageSize}&${str}`);
    },
    // 重置
    resetForm(formName) {
      for(var i in this.ruleForm){
        this.ruleForm[i] = '';
      }
      this.region = '';
      this.$refs[formName].resetFields();
    },
    // 修改信息内容
    // http://10.236.44.188:8080/sysconfig/modify.do?id=1&enumName=1&enumValue=1.1&gameName=game1&gameId=201&description=1111
    revampMsg() {
      var obj = this.revampForm;
      this.$http.post(`${this.SRC}sysconfig/modify.do`,obj,{emulateJSON:true}).then(
        function(res){
          if(res.body['code'] === 0){
            this.successFun(res.body["msg"]);
            this.getPageData(`curPage=${this.currentPage}&pageSize=${this.pageSize}`);
          }else{
            this.errorFun(res.body["msg"])
          }
        }
      )
    },
		revamp(formName) {
			this.$refs[formName].validate((valid) => {
				if (valid) {
					this.revampMsg();
				} else {
					console.log('error submit!!');
					return false;
				}
			});
		},
    revampReset(formName) {
      for(var i in this.revampForm){
        this.revampForm[i] = '';
      }
      this.revampRegion = '';
      this.$refs[formName].resetFields();
    },
    // 获取数据 获取工程List 以及页面展示数据
    getProjectData(){
      this.projects = [];
      this.$http.post(`${this.SRC}project/list.do`).then(
        function(res){
          var data = res.body;
          console.log(data)
          if(data["code"] === 0){
            for(var i=0; i<data['msg'].length; i++){
              var obj = {
                label: data["msg"][i]['projectName'],
                value: data["msg"][i]['id']
              }
              this.projects.push(obj);
              console.log(obj)
            }
          }else{
            this.errorFun(data['msg'])
          }
        }
      )
    },
    //获取页面数据
    getPageData(str) {
      var that = this;
      var p = new Promise(
        function(resovle,reject) {
          that.$http.post(`${that.SRC}sysconfig/list.do?${str}`).then(
            function(res){
              resovle(res);
            }
          )
        }
      ) 
      p.then(function(value){
        that.tableData = [];
        var data = value.body;
        if(data['code']===0){
          that.tableData = data.msg.data;
          that.dataTotal = data.msg.totalSize;
        }else{
          that.errorFun(data['msg'])
        }
      })
    }
	},
	mounted() {
    this.SRC = this.$store.getters.src3;
    // 获取工程的名字
    this.getProjectData();
    // 获取页面展示数据
    this.getPageData(`curPage=${this.currentPage}&pageSize=${this.pageSize}`);
		this._init();
	}
}